package net.entelijan.cobean.examples.showcase.config.toggle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import net.entelijan.cobean.data.literature.LitBook;
import net.entelijan.config.CobeanConfigFactory;
import net.entelijan.config.ICobeanConfig;
import net.entelijan.scf.IShowcase;

public class ConfigBookToggleShowcase implements IShowcase<ConfigBookTogglePanel> {

	private LitBook book;

	public ConfigBookToggleShowcase() {
		super();
	}

	@Override
	public void init(final ConfigBookTogglePanel view) {

		book = createBook();

		// Bind the view to the model using convetion over configuration.
		ICobeanConfig cfg = CobeanConfigFactory.createConfig();
		book = cfg.bind(book, view, null);

		view.getShowModelButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(view, "" + book);
			}

		});

		view.getDefaultValuesButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				setDefaultValues(book);
			}

		});

	}

	private LitBook createBook() {
		LitBook re = new LitBook();
		setDefaultValues(re);
		return re;
	}

	private void setDefaultValues(LitBook book) {
		book.setId(0L);
		book.setTitle("Der Oger");
		book.setSubtitle("");
		book.setTranslation(true);
		book.setYear(1986);
	}

	public LitBook getBook() {
		return book;
	}

	public void setBook(LitBook book) {
		this.book = book;
	}

}
